<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./OL_SDK/include-openlayers-local.js"></script>
  <script src="./libs/gaode.js"></script>
  <script src="https://lib.baomitu.com/jquery/2.2.4/jquery.js"></script>
  <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
  <script src="./utils/setMarkerStyle.js"></script>
  <link rel="stylesheet" href="//at.alicdn.com/t/c/font_4187048_49v30yfzxgg.css">
  <style>
    * {
      margin: 0;
      padding: 0
    }

    #map {
      width: 100vw;
      height: 100vh;
    }

    #location {
      width: 200px;
      height: 50px;
      z-index: 100;
      position: fixed;
      left: 50px;
      top: 11px;
      background-color: #6528e0;
      border: none;
      border-radius: 15px;
      color: #fff;
    }
  </style>
</head>

<body>
  <button id="location">
    <i class="iconfont">&#xe83d;</i>
    <span>default</span>
  </button>
  <div id="map">
  </div>
  <script>
    var map = new ol.Map({
      target: "map",
      layers: [gaode],
      view: new ol.View({
        projection: 'EPSG:4326',
        center: [114.30, 30.50],
        zoom: 4
      })
    })
    /* 在城市的中心点设置标注 */
    var url = 'https://restapi.amap.com/v3/ip?key=db774d5285ed8eb97c1d816f8e671c94'
    /* 
    1、ajax请求获取数据 -center
     */
    /* 2、设置放置中心点的图层 */
    var mid_source = new ol.source.Vector({

    })
    var mid_layer = new ol.layer.Vector({
      source: mid_source,
      style: setMarkerStyle('/images/位置.png'),
      zIndex: 100
    })
    map.addLayer(mid_layer)

    const style = new ol.style.Style({
      // 形状
      fill: new ol.style.Fill({
        color: 'rgba(0,0,0,0.5)'
      }),
      /* 描边 */
      stroke: new ol.style.Stroke({
        color: '#ff2d51',
        width: 2
      }),
      image: new ol.style.Circle({
        radius: 20,
        /* 填充色 */
        fill: new ol.style.Fill({
          color: 'rgba(0,0,0,0.5)'
        }),
        /* 描边 */
        stroke: new ol.style.Stroke({
          color: '#ff2d51',
          width: 2
        })
      })
    })

    $.ajax({
      url,
      success: data => {
        let { adcode, rectangle, city } = data;
        $("#location span").html(city)
        /* rectangle字符串转化数组 */
        var arr = rectangle.split(";");
        var result = arr.map(item => item.split(","));
        /* turf库算出中心 */
        var p1 = turf.point(result[0]);
        var p2 = turf.point(result[1]);
        var mid_point = turf.midpoint(p1, p2)

        /* geojson转化成openlayer要素 */
        let point = new ol.format.GeoJSON().readFeatures(mid_point);
        mid_source.addFeatures(point)
        /* 3、飞行到中心点 */
        let center = mid_point.geometry.coordinates;
        map.getView().animate({
          center,
          zoom: 8,
          duration: 2000
        })
        /* 4、加载geojson --阿里 */
        let geo_url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}.json`
        var geo_source = new ol.source.Vector({
          format: new ol.format.GeoJSON(),
          url: geo_url
        })
        var geo_layer = new ol.layer.Vector({
          source: geo_source,
          style
        })
        map.addLayer(geo_layer)
      }
    })
  </script>
</body>

</html>